//
//  SSDKUIManager.m
//  SuperSDK
//
//  Created by wyht－ios－dev on 16/9/1.
//  Copyright © 2016年 WYHT. All rights reserved.
//

#import "SSDKUIManager.h"

@implementation SSDKUIManager

static SSDKUIManager *instance = nil;

+(id) getInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        if(instance.indicator==nil)
            instance.indicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    });
    return instance;
}

+(void) showLoadingUI {
    if([[SSDKUIManager getInstance] indicator]!=nil) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[[SSDKUIManager getInstance] indicator] startAnimating];
            [[[SSDKUIManager getInstance] indicator] setColor:[UIColor blackColor]];
            [[SSDKUIManager getInstance] indicator].center=[SSDKUIManager getCurrentVC].view.center;
            [[SSDKUIManager getCurrentVC].view addSubview:[[SSDKUIManager getInstance] indicator]];
        });
    }
}

+(void) dismissLoadingUI {
    if([[SSDKUIManager getInstance] indicator]!=nil) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[[SSDKUIManager getInstance] indicator] stopAnimating];
            [[[SSDKUIManager getInstance] indicator] removeFromSuperview];
        });
    }
}

//获取当前的ViewController
+(UIViewController *) getCurrentVC {
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

@end
